<?php
    
    /*
        definisce box ed altro per la gestione dei post come contenuti
     
    */

define('WPF_CONTENUTO_POSTS','wpf_contenuto_post');

    
function wpf_contenuto_box() {
global $post;
  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), 'wpf_contenuto_noncename' );
   
    $val_moduli = get_posts(wpf_get_query('moduli_approvati'));  
    $values = '';
    foreach($val_moduli as $id => $val ) {
        $values .= $val->ID.':'.$val->post_title.',';
    }
    $values = trim($values,',');

   
  // The actual fields for data entry 
    admin_field('is_content','checkbox','Is a content', get_post_meta($post->ID,'is_content',true), WPF_DOMAIN,'Check box if this '.$post->post_type.' must be a module content.');
    admin_field('wpf_module','select','Is content for|'.$values, get_post_meta($post->ID,'wpf_module',true), WPF_DOMAIN,'Select module linked with this '.$post->post_type);

    
}


function wpf_contenuto_save($post_id) {
   if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {    	
        return;
    }
    
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
   /* if ( !wp_verify_nonce( @$_POST['wpf_contenuto_noncename'], plugin_basename( __FILE__ ) ) ) {     	
          return;
    }*/
          


    admin_field_save_post_meta($post_id,'is_content',@$_POST['is_content']=='on');
    admin_field_save_post_meta($post_id,'wpf_module',$_POST['wpf_module']);
    
}

function wpf_contenuto_settings_page() {
//@TODO devo verificare se il posttype ha la tassonomia necessaria, altrimenti la creo 

    //must check that the user has the required capability 
    if (!current_user_can('manage_options'))   {
	wp_die( __('You do not have sufficient permissions to access this page.') );
    }
    
    // save

    $readed_val_array = @$_POST['cb'];
    if ($readed_val_array!='')  update_option(WPF_CONTENUTO_POSTS, serialize($readed_val_array));	      
    
    // read
    admin_panel('wpf_content_options',
                'admin.php?page=wpf_contenuto_options',
                'Opzioni di contenuto',
                'Opzioni per la gestione dei contenuti',
                'ver. 00',
                WPF_DOMAIN);
    $opt_val_array = maybe_unserialize(get_option( WPF_CONTENUTO_POSTS));
    $post_types=get_post_types(array('public'   => true ),'object');
    
    echo __('<h3>Post type abilitati per i contenuti</h3>');
    
    
    foreach ($post_types  as $post_type ) {
        @admin_field("cb[".$post_type->name."]", 'checkbox', $post_type->label, $opt_val_array[$post_type->name]=='on', WPF_DOMAIN, '');
    }
    
    admin_panel_close();
}


	


function wpf_contenuto_add_boxes($screen) {
    $opt_val_array = maybe_unserialize(get_option( WPF_CONTENUTO_POSTS));
    if ($screen->post_type!='') {
		var_dump($opt_val_array);
        if (@$opt_val_array[$screen->post_type]=='on')
            add_meta_box( 'wpf_contenuto', 'Contenuto', 'wpf_contenuto_box', $screen->post_type, 'side', 'core', '' );            
    }
}

function wpf_contenuto_hook() {

    add_action('current_screen','wpf_contenuto_add_boxes');
    //add_action('admin_init','wpf_contenuto_admin');
    
    add_action( 'save_post', 'wpf_contenuto_save' ); // salva i dati di della scheda
    
    
}

wpf_contenuto_hook();

?>
